{
 "metadata": {
  "name": "",
  "signature": "sha256:7922e95e19c2edf0b7a2a8c6bb49659489498fbe0e3bdcb627161e949cf6eeb8"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Collaborators: Alan Wu, Fred Landis\n",
      "\n",
      "#Problem 1\n",
      "\n",
      "#We can see that there exist $4^{(6/2)} = 4^3 = 64$ different reverse palindromic recognition sites of length 6. \n",
      "#This is because, as we imagine constructing the palindromic sequence, we have four possible options for the first entry, but any of these choices immediately fixes the last entry. \n",
      "#This process continues for $n/2$ entries where $n$ is the total length of interest. \n",
      "#We thus see that the problem is reduced to finding the possible combinations of half the original string length. \n",
      "#The fraction of $2N$-mers, that are reverse palindromic is $\\frac{4^N}{4^{2N}} = \\frac{1}{4^N}$. \n",
      "#We see this as the numerator (before simplification) is the number of possible reverse palindromes in a $2N$-mer, and the denominator (before simplification) is all possible strings for a $2N$-mer.  \n",
      "#The following code produces all such possible recognition sites: \n",
      "\n",
      "import itertools\n",
      "\n",
      "sizeOfPalindrome = 6\n",
      "reversePalindromes = []\n",
      "\n",
      "#essentially performs a \"repeat\"-dimensional cartesian product\n",
      "halfPermutations = itertools.product('ATGC',repeat=sizeOfPalindrome/2) \n",
      "\n",
      "for permutation in halfPermutations:\n",
      "\treversePalindromes.append(''.join(permutation)+''.join(reversed(['A' if b=='T' else 'C' if b=='G' else 'T' if b=='A' else 'G' for b in permutation])))\n",
      "print reversePalindromes\n",
      "\n",
      "#The following reverse palidromes were produced by my code: \n",
      "#['AAATTT', 'AATATT', 'AAGCTT', 'AACGTT', 'ATATAT', 'ATTAAT', 'ATGCAT', 'ATCGAT', 'AGATCT', 'AGTACT', \n",
      "#'AGGCCT', 'AGCGCT', 'ACATGT', 'ACTAGT', 'ACGCGT', 'ACCGGT', 'TAATTA', 'TATATA', 'TAGCTA', 'TACGTA', \n",
      "#'TTATAA', 'TTTAAA', 'TTGCAA', 'TTCGAA', 'TGATCA', 'TGTACA', 'TGGCCA', 'TGCGCA', 'TCATGA', 'TCTAGA', \n",
      "#'TCGCGA', 'TCCGGA', 'GAATTC', 'GATATC', 'GAGCTC', 'GACGTC', 'GTATAC', 'GTTAAC', 'GTGCAC', 'GTCGAC', \n",
      "#'GGATCC', 'GGTACC', 'GGGCCC', 'GGCGCC', 'GCATGC', 'GCTAGC', 'GCGCGC', 'GCCGGC', 'CAATTG', 'CATATG', \n",
      "#'CAGCTG', 'CACGTG', 'CTATAG', 'CTTAAG', 'CTGCAG', 'CTCGAG', 'CGATCG', 'CGTACG', 'CGGCCG', 'CGCGCG', \n",
      "#'CCATGG', 'CCTAGG', 'CCGCGG', 'CCCGGG']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "['AAATTT', 'AATATT', 'AAGCTT', 'AACGTT', 'ATATAT', 'ATTAAT', 'ATGCAT', 'ATCGAT', 'AGATCT', 'AGTACT', 'AGGCCT', 'AGCGCT', 'ACATGT', 'ACTAGT', 'ACGCGT', 'ACCGGT', 'TAATTA', 'TATATA', 'TAGCTA', 'TACGTA', 'TTATAA', 'TTTAAA', 'TTGCAA', 'TTCGAA', 'TGATCA', 'TGTACA', 'TGGCCA', 'TGCGCA', 'TCATGA', 'TCTAGA', 'TCGCGA', 'TCCGGA', 'GAATTC', 'GATATC', 'GAGCTC', 'GACGTC', 'GTATAC', 'GTTAAC', 'GTGCAC', 'GTCGAC', 'GGATCC', 'GGTACC', 'GGGCCC', 'GGCGCC', 'GCATGC', 'GCTAGC', 'GCGCGC', 'GCCGGC', 'CAATTG', 'CATATG', 'CAGCTG', 'CACGTG', 'CTATAG', 'CTTAAG', 'CTGCAG', 'CTCGAG', 'CGATCG', 'CGTACG', 'CGGCCG', 'CGCGCG', 'CCATGG', 'CCTAGG', 'CCGCGG', 'CCCGGG']\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Problem 2\n",
      "\n",
      "#The frequencies of the palindromic sequences in the dataset are produced by the following code. \n",
      "#NOTE: HumChr01.seq must be in this file's path\n",
      "\n",
      "#It appears as though the more frequently occurring sequences are those that are made of predominately 'T's and 'A's, \n",
      "#while the less frequently occurring recognition sites are those that are made of predominately 'G's and 'C's. \n",
      "\n",
      "\n",
      "with open(\"HumChr01.seq\", 'r') as sequenceFile:\n",
      "\tfullSequence = sequenceFile.read()\n",
      "\n",
      "reversePalindroneFrequencies = {}\n",
      "\n",
      "for reversePalindrone in reversePalindromes:\n",
      "\treversePalindroneFrequencies[reversePalindrone] = fullSequence.count(reversePalindrone)\n",
      "\n",
      "print reversePalindroneFrequencies\n",
      "\n",
      "#The following frequencies were produced by my code: \n",
      "#{'ACTAGT': 30370, 'TATATA': 144759, 'TCGCGA': 1127, 'TACGTA': 9337, \n",
      "#'CGGCCG': 7651, 'CTTAAG': 50087, 'TGCGCA': 7005, 'CCTAGG': 48979,\n",
      "# 'CTCGAG': 9957, 'GAGCTC': 50049, 'ACATGT': 79458, 'TTCGAA': 8091, \n",
      "#'ATTAAT': 106729, 'AGTACT': 42704, 'AGATCT': 60179, 'ACGCGT': 1631, \n",
      "#'CGTACG': 839, 'GGCGCC': 19486, 'AGCGCT': 8579, 'TGGCCA': 107911, \n",
      "#'ATCGAT': 6471, 'TGTACA': 61405, 'TGATCA': 56893, 'GACGTC': 5317, \n",
      "#'TTTAAA': 252188, 'AAGCTT': 64394, 'GCTAGC': 22197, 'CATATG': 66900, \n",
      "#'CTATAG': 39287, 'TCATGA': 74262, 'AACGTT': 11841, 'CTGCAG': 105663, \n",
      "#'ATGCAT': 66729, 'GCATGC': 43792, 'TAGCTA': 46642, 'AATATT': 170369, \n",
      "#'CGCGCG': 3460, 'CACGTG': 20824, 'CCGCGG': 5530, 'TCCGGA': 7704, \n",
      "#'ATATAT': 161388, 'GATATC': 32152, 'CAATTG': 40935, 'CGATCG': 978, \n",
      "#'ACCGGT': 4216, 'AAATTT': 192451, 'CCCGGG': 31941, 'GCCGGC': 10460, \n",
      "#'GGGCCC': 40294, 'GTGCAC': 38168, 'GTATAC': 31467, 'GTTAAC': 29507, \n",
      "#'TAATTA': 101323, 'GGTACC': 22705, 'GCGCGC': 5048, 'CAGCTG': 89432, \n",
      "#'GAATTC': 60248, 'TTATAA': 120196, 'GGATCC': 30113, 'TTGCAA': 68694, \n",
      "#'GTCGAC': 2342, 'TCTAGA': 61946, 'AGGCCT': 67067, 'CCATGG': 59851}"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Problem 3\n",
      "\n",
      "#The following code produces a histogram which displays the distribution of lengths of the segments of our sample when cut at recognition sites \"CGTAC/G\". \n",
      "#That is, when the sequence of nucleotides CGTACG are found, a cut is made between the last 'C' and 'G'. \n",
      "\n",
      "import matplotlib.pyplot\n",
      "import re \n",
      "\n",
      "cutStrands = re.sub(\"(?<=CGTAC)(?=G)\",\"jaretrulz\",fullSequence).split(\"jaretrulz\") #super hacky because re.split sucks\n",
      "matplotlib.pyplot.hist([len(s) for s in cutStrands], bins=100, range=(0,3000000))\n",
      "matplotlib.pyplot.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "HapllLengths = [68, 114, 133, 162, 387, 557, 649, 813, 1737, 2012, 2325, 7342]\n",
      "BstUILengths = [235, 316, 1403, 1455, 1562, 1589, 1633, 1666, 6440]\n",
      "HapllAndBstUILengths = [9, 68, 99, 114, 133, 136, 162, 183, 387, 513, 557, 754, 813, 1032, 1455, 1562, 1633, 1666, 2012, 3011]\n",
      "\n",
      "def powerSet(inputSet):\n",
      "\tpowerSet = [[]]\n",
      "\tfor element in inputSet:\n",
      "\t\tpowerSet.extend([subset + [element] for subset in powerSet])\n",
      "\treturn powerSet\n",
      "\n",
      "def determineOrderOfSegments(digestionSetA, digestionSetB, combinedDigestionSets):\n",
      "\tpowerSetHits = []\n",
      "\tfor setToTry in powerSet(combinedDigestionSets):\n",
      "\t\tif sum(setToTry) in digestionSetA + digestionSetB:\n",
      "\t\t\tpowerSetHits.extend([setToTry])\n",
      "\t\n",
      "\tsolutionFound = False\n",
      "\n",
      "\tremainingLengthsToExplain = [\"init\"]\n",
      "\n",
      "\twhile not remainingLengthsToExplain:\n",
      "\t\tremainingLengthsToExplain = digestionSetA + digestionSetB\n",
      "\t\tusedPowerSetElements = []\n",
      "\t\tfor powerSetHit in powerSetHits:\n",
      "\t\t\tfor powerSetElement in powerSetHit:\n",
      "\t\t\t\tif powerSetElement in usedPowerSetElements:\n",
      "\t\t\t\t\tbreak\n",
      "\t\t\tremainingLengthsToExplain.remove(sum(powerSetHit))\n",
      "\t\t\tusedPowerSetElements.extend(powerSetHit)\n",
      "\t\tprint sorted(usedPowerSetElements)\n",
      "\n",
      "determineOrderOfSegments(HapllLengths, BstUILengths, HapllAndBstUILengths)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 11
    }
   ],
   "metadata": {}
  }
 ]
}